package com.house.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.house.common.R;
import com.house.entity.Role;
import com.house.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 保存角色
     * @param role
     * @return
     */
    @PostMapping("/save")
    public R<String> save(@RequestBody Role role) {
        roleService.save(role);
        return R.success("添加成功");
    }
    /**
     * 获取角色分页
     * @param page
     * @param pageSize
     * @param roleName
     * @return
     */
    @GetMapping("/getRoles")
    public R<Page<Role>> getUsers(int page, int pageSize, String roleName) {
        Page<Role> pageInfo = roleService.selectPage(page, pageSize, roleName);
        return R.success(pageInfo);
    }

    /**
     * 根据id获取角色
     * @param roleId
     * @return
     */

    @GetMapping("/getRoleByRoleId/{roleId}")
    public R<Role> getRoleByRoleId(@PathVariable int roleId) {
        Role role = roleService.getRoleByRoleId(roleId);
        return R.success(role);
    }

    /**
     * 根据id获取角色
     * @param roleId
     * @return
     */

    @GetMapping("/getRoleMenuIdsByRoleId/{roleId}")
    public R<String> getRoleMenuIdsByRoleId(@PathVariable int roleId) {
        Role role = roleService.getRoleMenuIdsByRoleId(roleId);
        return R.success(role.getMenuIds());
    }

    /**
     * 更新角色
     * @param role
     * @return
     */

    @PutMapping("/update")
    public R<String> update(@RequestBody Role role) {
        roleService.update(role);
        return R.success("更新成功!");
    }

    /**
     * 删除角色
     * @param roleId
     * @return
     */

    @DeleteMapping("/delete/{roleId}")
    public R<String> delete(@PathVariable int roleId) {
        int res = roleService.delete(roleId);
        if (res == 0) {
            return R.error("删除失败!");
        }
        return R.success("删除成功!");
    }

    /**
     * 角色绑定菜单
     * @param role
     * @return
     */

    @PutMapping("/bindMenus")
    public R<String> bindMenus(@RequestBody Role role) {
        roleService.bindMenus(role);
        return R.success("更新成功!");
    }
}
